<?php
include("database.class.php");

$db = new Database("../mysqlconfig.txt");
$link = $db->connectWithDb();

if($_SERVER['REQUEST_METHOD'] != "POST") {
?>
<h1>Register</h1>
<form method="POST">
	<table>
	<tr><td>Username:</td><td><input type="text" name="name"></td></tr>
	<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
	<tr><td>Email:</td><td><input type="email" name="email"></td></tr>
	<tr><td colspan="2"><input type="submit" name="submit" value="Register me!"></td></tr>
	</table>
</form>
<?php
}
else { // posted
	$name = $_POST['name'];
	$pass = md5($_POST['pass']);
	$email = $_POST['email'];
	
	if(!preg_match("/^([-0-9a-zA-Z_]){4,15}$/", $name)) {
		die("ERROR: Please give a name that will pass '^([-0-9a-Z_]){4,15}$'.");
	}
	
	if(!preg_match('/^[A-Za-z0-9\+._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,6}$/', $email)) {
		die("ERROR: Please give a valid email address.");
	}
	
	$ip = $_SERVER['REMOTE_ADDR'];
	
	$ret = $db->executeQuery($link, "INSERT INTO users (name, pass, email, ip) VALUES ('$name', '$pass', '$email', '$ip')");
	
	if($ret) {
		echo "You are successfully registered";
	}	
}
?>
